/**
 *
 * @author John Creecy
 */
package battlebarnapplet;

import battlebarnapplet.entities.ClientProjectileEntity;
import battlebarnapplet.entities.LocationEntity;
import battlebarnapplet.entities.PlayerMovementEntity;
import battlebarnapplet.entities.VelocityEntity;
import battlebarnserver.entities.ProjectileEntity;
import battlebarnserver.util.Constants;
import battlebarnserver.util.Conversions;
import battlebarnserver.util.Resources;

import java.io.IOException;
import java.net.*;
import java.util.logging.Level;
import java.util.logging.Logger;

public class StateListener implements Runnable {

	public StateListener() {
	}

	public void run() {
		socketMonitor();
	}

	public void socketMonitor() {
		try {
			String packetData = "";
			long packetTimeStamp = 0;
			String playerName;
			LocationEntity locationEntity;
			LocationEntity previousLocationEntity;
			VelocityEntity velocityEntity;
			ClientProjectileEntity clientProjectileEntity;
			DatagramPacket  receivePacket;
			PlayerMovementEntity playerMovementEntity;
			PlayerMovementEntity previousPlayerMovementEntity;
			byte[] receiveData;
			int xLoc = 0;
			int yLoc = 0;
			int facing;
			int playerHealth;
			URL shotSound = this.getClass().getResource("assets/sounds/shot.wav");
			URL hitSound = this.getClass().getResource("assets/sounds/hit.wav");
			DatagramSocket serverSocket = new DatagramSocket(ClientStart.clientPort);
			while (true) {
				receiveData = new byte[1024];
				try {
					receivePacket = new DatagramPacket(receiveData, receiveData.length);
					serverSocket.receive(receivePacket);
					packetData = new String(receivePacket.getData());
					packetTimeStamp = Long.parseLong(packetData.substring(0, 13));
					//if (packetTimeStamp > Board.lastPacketTime) {
					xLoc = Integer.parseInt(packetData.substring(13, 16));
					yLoc = Integer.parseInt(packetData.substring(16, 19));
					// inanimates
					if (packetData.substring(19, 20).equals("i")) {
						locationEntity = new LocationEntity(xLoc, yLoc, 2, "i",0);
						Board.inanimateLocs.add(locationEntity);
					// new projectiles
					} else if (packetData.substring(19, 20).equals("P")) {
						velocityEntity =  Conversions.getVelocityPair(Integer.parseInt(packetData.substring(20, 21)));
						clientProjectileEntity = new ClientProjectileEntity(packetData.substring(21),velocityEntity.getVelocityX(),velocityEntity.getVelocityY(),xLoc,yLoc);
						Board.projectiles.put(packetData.substring(21),clientProjectileEntity);
						Runnable runnable = new SoundEffectThread(shotSound);
						Thread thread = new Thread(runnable);
						thread.start();
					// AI units
					} else if (packetData.substring(24, 26).equals("AI")) {
						playerName = packetData.substring(24);
						playerHealth = Integer.parseInt(packetData.substring(21, 24));
						locationEntity = new LocationEntity(xLoc, yLoc, Integer.parseInt(packetData.substring(19,20)), playerName, playerHealth);
						Board.enemyLocs.put(playerName, locationEntity);
					// Event triggers
					} else if (packetData.substring(19, 20).equals("E")) {
						if(packetData.substring(20, 21).equals("1")) {
							Board.removeProjectiles.add(packetData.substring(21));
							Runnable runnable = new SoundEffectThread(hitSound);
							Thread thread = new Thread(runnable);
							thread.start();
						} 
						if(packetData.substring(20, 21).equals("2")) {
							Board.winnerName = packetData.substring(21);
						}
					}
					// player updates
					else {
						playerName = packetData.substring(24);
						playerHealth = Integer.parseInt(packetData.substring(21, 24));
						facing = Integer.parseInt(packetData.substring(19,20));
						locationEntity = new LocationEntity(xLoc, yLoc, facing, playerName, playerHealth);
						Board.playerLocs.put(playerName, locationEntity);	
					}

					BattleBarnApplet.lastPacketTime = packetTimeStamp;
					//}
				} catch (SocketException ex) {
					System.out.println(ex);
				} catch (IOException ex) {
					System.out.println(ex);
				}
			}
		} catch (SocketException ex) {
			Logger.getLogger(StateListener.class.getName()).log(Level.SEVERE, null, ex);
		}
	}
}
